﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tilver.Coder;
using Tilver.Coder.Core;
using Microsoft.Data.ConnectionUI;
using System.IO;
using Tilver.DBConnectionUI;

namespace Tilver.Coder.Win.Dialogs
{
    public partial class CreateProjectDialog : Form
    {
        #region 属性
        /// <summary>
        /// 项目信息
        /// </summary>
        public CoderProject Project { get; set; }
        #endregion

        public CreateProjectDialog()
        {
            InitializeComponent();
            #region 初始化数据
            cmbDataProvider.DataSource = Enum.GetValues(typeof(DataProviderTypeEnum));
            cmbDataProvider.SelectedItem = DataProviderTypeEnum.MSSQL;
            txtSaveDirectory.Text = @".\DAL";
            folderBrowserDialog_SaveDirectory.SelectedPath = Directory.GetCurrentDirectory();

            #endregion
        }

        #region 获取项目保存路径
        private void btnGetSaverDirectory_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog_SaveDirectory.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtSaveDirectory.Text = folderBrowserDialog_SaveDirectory.SelectedPath;
            }
        }
        #endregion
        #region  生成数据数据库连接串
        private void btnCreateConnectionString_Click(object sender, EventArgs e)
        {
            var dialog = new DBConnectionDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtConnectionString.Text = dialog.ConnectionString;
            }
        }
        #endregion
        #region 取消创建项目
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
        #endregion
        #region 确定创建项目
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider_Project.Clear();
                if (txtProjectName.Text.Trim().Length == 0)
                {
                    errorProvider_Project.SetError(txtProjectName, "项目名称不能为空");
                    return;
                }

                if (txtAuthor.Text.Trim().Length == 0)
                {
                    errorProvider_Project.SetError(txtAuthor, "创建者不能为空");
                    return;
                }

                if (txtNameSpace.Text.Trim().Length == 0)
                {
                    errorProvider_Project.SetError(txtNameSpace, "命名空间不能为空");
                    return;
                }

                if (txtSaveDirectory.Text.Trim().Length == 0)
                {
                    errorProvider_Project.SetError(txtSaveDirectory, "文件夹不能为空");
                    return;
                }

                if (!Directory.Exists(txtSaveDirectory.Text.Trim()))
                {
                    try
                    {
                        Directory.CreateDirectory(txtSaveDirectory.Text.Trim());
                    }
                    catch (Exception ex)
                    {
                        errorProvider_Project.SetError(txtSaveDirectory, string.Format("文件夹路径无效：{0}", ex.Message));
                        return;
                    }
                }

                if (txtConnectionString.Text.Trim().Length == 0)
                {
                    errorProvider_Project.SetError(txtConnectionString, "连接字符串不能为空");
                    return;
                }

                Project = new CoderProject
                {
                    DataBases = new List<CoderDataBase>
                    {
                        new CoderDataBase
                        {
                        ConnectionString = txtConnectionString.Text,
                        DataProviderType =(DataProviderTypeEnum)cmbDataProvider.SelectedItem
                        }
                    },
                    Name = txtProjectName.Text,
                    Author = txtAuthor.Text,
                    CreateDate = DateTime.Now,
                    ModifyDate = DateTime.Now,
                    NameSpace = txtNameSpace.Text,
                    SaveDirectory = txtSaveDirectory.Text
                };
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        #endregion
        #region 设置项目信息
        //public void SetProjectInfo(CoderProject projectInfo)
        //{
        //    if (projectInfo != null)
        //    {
        //        txtAuthor.Text = projectInfo.Author;
        //        txtConnectionString.Text = projectInfo.DataBases.ConnectionString;
        //        txtNameSpace.Text = projectInfo.NameSpace;
        //        txtProjectName.Text = projectInfo.Name;
        //        txtSaveDirectory.Text = projectInfo.SaveDirectory;
        //        cmbDataProvider.SelectedItem = projectInfo.DataBases.DataProviderType;
        //    }
        //}
        #endregion

    }

    #region 数据库连接字符串生成器
    /// <summary>
    /// 数据库连接字符串生成器
    /// </summary>
    public class  DBConnectionDialog
    {
        private Tilver.DBConnectionUI.ConnectionStringDialog dialog = new ConnectionStringDialog();
       
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString
        {
            get { return dialog.ConnectionString; }
        }
        /// <summary>
        /// 创建DBConnectionDialog实例
        /// </summary>
        public DBConnectionDialog()
        {
        }
        
        /// <summary>
        /// 显示默认数据库连接字符串生成器
        /// </summary>
        /// <returns>DialogResult.OK or DialogResult.Cancle</returns>
        public DialogResult ShowDialog()
        {
            return dialog.ShowDialog();
        }
    }

    /*
    /// <summary>
    /// 数据库连接字符串生成器
    /// </summary>
    public class DBConnectionDialog
    {
        private DataConnectionDialog dialog = new DataConnectionDialog();
        
        /// <summary>
        /// 数据源
        /// </summary>
        public DataSource DataSource
        {
            get { return dialog.SelectedDataSource; }
        }
        /// <summary>
        /// 数据提供信息
        /// </summary>
        public DataProvider DataProvider
        {
            get { return dialog.SelectedDataProvider; }
        }
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString
        {
            get { return dialog.ConnectionString; }
        }
        /// <summary>
        /// 创建DBConnectionDialog实例
        /// </summary>
        public DBConnectionDialog()
        {
            dialog.SaveSelection = true;
            DataSource.AddStandardDataSources(dialog);
            
            dialog.SelectedDataSource = DataSource.SqlDataSource;
            dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
        }
        /// <summary>
        /// 根据提供的参数显示数据库连接字符串生成器
        /// </summary>
        /// <param name="dataSource">数据源</param>
        /// <param name="dataProvider">数据提供信息</param>
        /// <param name="connectionString">连接字符串</param>
        /// <returns>DialogResult.OK or DialogResult.Cancle</returns>
        public DialogResult ShowDialog(DataSource dataSource, DataProvider dataProvider, string connectionString)
        {
            dialog.SelectedDataSource = dataSource;
            dialog.SelectedDataProvider = dataProvider;
            dialog.ConnectionString = connectionString;
            return ShowDialog();
        }

        /// <summary>
        /// 根据提供的参数显示数据库连接字符串生成器
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public DialogResult ShowDialog(DBConnectionResult result)
        {
            return ShowDialog(result.DataSource, result.DataProvider, result.ConnectionString);
        }

        /// <summary>
        /// 显示默认数据库连接字符串生成器
        /// </summary>
        /// <returns>DialogResult.OK or DialogResult.Cancle</returns>
        public DialogResult ShowDialog()
        {
            return DataConnectionDialog.Show(dialog);
        }
    }
    */

    /// <summary>
    /// DBConnectionDialog所选择的结果
    /// </summary>
    public class DBConnectionResult
    {
        private DataSource _dataSource;
        /// <summary>
        /// 数据源
        /// </summary>
        public DataSource DataSource
        {
            get { return _dataSource; }
            set { _dataSource = value; }
        }
        private DataProvider _dataProvider;
        /// <summary>
        /// 数据提供程序
        /// </summary>
        public DataProvider DataProvider
        {
            get { return _dataProvider; }
            set { _dataProvider = value; }
        }
        private string _connectionString;
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

    }
    #endregion
}
